PyTorch Logistic Regression

Dataset

a random n-class classification dataset can be generated using sklearn.datasets.make_classification. Here, we generate a dataset with two features and 6000 instances. Moreover, the dataset is generated for multiclass classification with five classes.

Modeling

Train and Test sets

One of the efficient methods of splitting a dataset into random train and test subsets is using sklearn.model_selection.train_test_split.

Logistic Regression

We have talked about the Logistic Regression using sklearn. Here, we would like to try the logistic regression iteratively using an optimization algorithm. The algorithm at each iteration uses the Cross-Entropy Loss to measure the loss, and then the gradient and the model update is calculated. At the end of this iterative process, we would reach a better level of agreement between test and predicted sets since the error would be lower from that of the first step.

Let's define some function by which we can analyze the performance of the modeling.

Model Optimization Plot

Confusion Matrix

The confusion matrix allows for visualization of the performance of an algorithm.

Plot Classification